package com.cpecc.api.controller;

import com.cpecc.manager.system.UserManager;
import com.cpecc.manager.wuzi.*;
import com.cpecc.model.system.User;
import com.cpecc.model.wuzi.BaoFei;
import com.cpecc.model.wuzi.BaoFeiSPR;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.wisteria.framework.web.APIResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/api")
public class APIController {

    @Autowired
    private UserManager userManager;

    @Autowired
    private WuZiManager wuZiManager;

    @Autowired
    private ShenQingManager shenQingManager;

    @Autowired
    private CaiGouSPRManager caiGouSPRManager;

    @Autowired
    private TuiHuoManager tuiHuoManager;

    @Autowired
    private HuanHuoManager huanHuoManager;

    @Autowired
    private WeiXiuManager weiXiuManager;

    @Autowired
    private YiJiaoManager yiJiaoManager;

    @Autowired
    private BaoFeiManager baoFeiManager;

    @Autowired
    private BaoFeiSPRManager baoFeiSPRManager;

    @Autowired
    private BuPeiManager buPeiManager;

    @RequestMapping(value = "/daiBan/zongShu", method = RequestMethod.GET)
    @ResponseBody
    public APIResult getWaitCount(@RequestParam(value = "userName") String userName) {
        User user = userManager.findByUsername(userName);
        if (user == null) {
            return APIResult.fail("账号[" + userName + "]不存在");
        }

        Set<String> roles = userManager.getRoles(userName);
        int count = 0;
        count += caiGouCount(user, roles);
        count += weiXiuCount(user, roles);
        count += yiJiaoCount(user, roles);
        count += baoFeiCount(user, roles);
        count += panDianCount(user, roles);
        count += tuiHuanCount(roles);
        count += tongBuCount(roles);
        return APIResult.success(count);
    }

    private int caiGouCount(User user, Set<String> roles) {
        int zongShu = 0;
        if (roles.contains("部门负责人")) {
            zongShu += caiGouSPRManager.getMySPList(user).size();
        }
        if (roles.contains("固资管理员")) {
            zongShu += shenQingManager.getAllYanShouRK().size();
            zongShu += shenQingManager.getAllCaiGouZT(106).size();
            zongShu += shenQingManager.getAllCaiGouZT(108).size();
            zongShu += tuiHuoManager.getAllTuiHuoSQ(303).size();
        }
        if (roles.contains("财务")) {
            zongShu += shenQingManager.getAllCaiGouZT(109).size();
        }
        if(userManager.isCGBMFZR(user)){
            zongShu+=shenQingManager.getAllCaiGouZT(103).size();
        }

        zongShu += shenQingManager.getAllCaiGouZT2CMR(104, user).size();
        zongShu += huanHuoManager.getAllHuanHuoSQ2CMR(1, user).size();
        zongShu += tuiHuoManager.getAllTuiHuoSQ2CMR(302, user).size();
        zongShu += shenQingManager.getAllLingYong(user).size();
        return zongShu;
    }

    private int weiXiuCount(User user, Set<String> roles) {
        int zongShu = 0;
        if(roles.contains("固资管理员")){
            zongShu+=weiXiuManager.getAllWeiXiuByZT(402).size();
            zongShu+=weiXiuManager.getAllWeiXiuByZT(405).size();
            zongShu+=weiXiuManager.getAllWeiXiuByZT(204).size();
        }
        zongShu+=huanHuoManager.getAllHuanHuoSQ2CMR(2, user).size();
        zongShu+=weiXiuManager.getMyWeiXiuQR(user).size();
        zongShu+=weiXiuManager.getMyWeiXiuRW(user).size();

        zongShu += weiXiuManager.getMyWeiXiuRW(user, 408).size();
        zongShu += weiXiuManager.getMyWeiXiuRW(user, 409).size();
        return zongShu;
    }

    private int yiJiaoCount(User user, Set<String> roles) {
        int zongShu = 0;
        if(roles.contains("固资管理员")){
            zongShu+=yiJiaoManager.getAllYiJiao(502).size();
            zongShu+=yiJiaoManager.getAllYiJiao(503).size();
            zongShu+=yiJiaoManager.getAllYiJiao(505).size();
        }
        if(roles.contains("财务")) {
            zongShu += yiJiaoManager.getAllYiJiao().size();
        }

        zongShu += yiJiaoManager.getMyYiJiaoRW(user, 507).size();
        zongShu += yiJiaoManager.getMyYiJiaoRW(user, 508).size();
        return zongShu;
    }

    private int baoFeiCount(User user, Set<String> roles) {
        int zongShu = 0;
        // 报废相关部门审批
        zongShu += baoFeiSPRManager.getBfxgbmspList(user).size();

        // 归口部门负责人审批
        zongShu += baoFeiSPRManager.getGKBMSPList(user).size();

        if (roles.contains("固资管理员")) {
            zongShu += baoFeiManager.getBaoFeiList(711).size();

            // 发起填报报废
            zongShu += baoFeiManager.getBaoFeiList(715).size();
        }

        if(roles.contains("部门负责人")){
            zongShu += baoFeiSPRManager.getMyBMSPList(user).size();
        }

        if(roles.contains("财务")){
            List<BaoFei> baoFeiChuLiCW = new ArrayList<>();
            List<BaoFeiSPR> sprList = baoFeiSPRManager.getBaoFeiList(user, 712);
            if (!CollectionUtils.isEmpty(sprList)) {
                for (BaoFeiSPR spr : sprList) {
                    baoFeiChuLiCW.add(spr.getBaoFei());
                }
            }
            zongShu += baoFeiChuLiCW.size();
        }
        return zongShu;
    }

    private int panDianCount(User user, Set<String> roles) {
        int zongShu = 0;
        if(roles.contains("固资管理员")){
            zongShu+=buPeiManager.getBuPeiList(802).size();
            zongShu+=buPeiManager.getBuPeiList(805).size();
        }
        if(roles.contains("财务")){
            zongShu+=buPeiManager.getBuPeiList(804).size();
        }
        zongShu+=wuZiManager.getMyPanDian(user).size();
        zongShu+=buPeiManager.getMyBuPeiList(user, 803).size();
        return zongShu;
    }

    private int tuiHuanCount(Set<String> roles) {
        int zongShu = 0;
        if(roles.contains("固资管理员")) {
            zongShu=yiJiaoManager.getAllTuiHui().size();
        }
        return zongShu;
    }

    private int tongBuCount(Set<String> roles) {
        int zongShu = 0;
        if(roles.contains("固资管理员")){
            zongShu+=userManager.getXinYongHu().size();
        }
        return zongShu;
    }
}






